package com.stars.empoly.vmodel

import android.app.Application
import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.view.View
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.hwangjr.rxbus.RxBus
import com.stars.empoly.componnent.CpAlipay
import com.stars.empoly.config.ConfigRx
import com.stars.empoly.model.UseOrderDetail
import com.stars.empoly.utils.net.NetEngine
import com.trailer.base.base.BRepository
import com.trailer.base.base.BViewModel
import kotlinx.coroutines.launch

class ReceivedDetailsVModel(application: Application) : BViewModel<BRepository>(application) {

    var id:String?=null

    var bean= MutableLiveData<UseOrderDetail>()

    var stars:Int=0
    //匿名
    var anonymous=MutableLiveData<Boolean>(false)//默认不匿名

    fun setTips(status:Int?,serviceTime:String?):String{

        when(status){
            -2->{
                return "技师已拒单 ($serviceTime)"
            }
            -1->{
                return "已取消 ($serviceTime)"
            }
            0->{
                return "待支付 ($serviceTime)"
            }
            1->{
                return "待接单 ($serviceTime)"
            }
            3->{
                return "订单已完成 ($serviceTime)"
            }

        }
        return ""
    }

    fun getMoneyType(payType:String?):String{

        when(payType){
            "1"->{
                return "余额支付"
            }
            "2"->{
                return "微信支付"
            }
            "3"->{
                return "支付宝支付"
            }
        }
        return ""
    }


    fun toCopyText(orderNo: String?){

        val cm: ClipboardManager? = repository().context?.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager?
        val mClipData = ClipData.newPlainText("Label", orderNo)
        cm?.setPrimaryClip(mClipData)
        toast("订单号已复制到粘贴板")

    }



    fun isComment(status:Int?,isComment:Int?):Int{

        if(status==3&&isComment==0){
            return View.VISIBLE
        }
        return View.GONE
    }



    fun setElavSubmit(){

        var isAnonymous="0"
        if(anonymous.value!!){
            isAnonymous="1"
        }
        viewModelScope.launch {
            var it=repository().lreq { NetEngine.service.comment(id,stars.toString(),isAnonymous) }
            if(it.ok(true)){
                //刷新
                getInfo()
            }
        }
    }




    fun getInfo(){
        viewModelScope.launch{
            var it=repository().lreq { NetEngine.service.OrderDetail(id) }
            if(it.ok()){
                bean.value=it.data
            }

        }
    }

    //--------------------------接单后

    fun setTTips(technicianStatus:Int?,serviceTime:String?):String{

        when(technicianStatus){

            1->{
                return "订单进行中($serviceTime)"
            }
            2->{
                return "订单进行中($serviceTime)"
            }
            3->{
                return "订单进行中($serviceTime)"
            }

        }

        return "订单进行中($serviceTime)"
    }

    fun toPay(type:String?){


        viewModelScope.launch {
            var it=repository().lreq { NetEngine.service.payOrder(id,type) }
            if(it.ok()){

                if(type == "1"){
                    toast("支付成功")

                    //刷新详情
                    getInfo()
                    //刷新列表
                    RxBus.get().post(ConfigRx.PLACE_ORDER, "")


                }
                if(type == "2"){
                    //微信支付

                }
                if(type == "3"){
                    val orderInfo = it.data?.alipay
                    CpAlipay.pay(repository().context!!, orderInfo!!)
                }

            }
        }

    }


}



